package com.rxmvp.baselibrary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * recyclerView的adapter，抽象了getHolder方法。
 *
 * 作者： huangRZ on 2017/1/21. 15:04
 * 邮箱：917647409@qq.com
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {

    protected  List<T> mData;           //item的数据
    protected final LayoutInflater mInflater;
    protected Context context;

    private OnItemClickListener mClickListener;

    private OnLongItemListener mLongListener;

    public BaseAdapter(Context ctx, List<T> list) {
        //如果传入null，创建一个空的list
        mData = list == null ? new ArrayList<T>() : list;
        mInflater = LayoutInflater.from(ctx);
        this.context = ctx;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final BaseViewHolder holder = getHolder(parent, viewType);
        //给item设置点击侦听
        if (mClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mClickListener.onItemClick(view, holder.getAdapterPosition());
                }
            });
        }
        //给item设置长按侦听
        if (mLongListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mLongListener.onLongItemClick(view, holder.getAdapterPosition());
                    return true;
                }
            });
        }
        return holder;
    }

    /** 获取holder对象，由子类实现 */
    protected abstract BaseViewHolder getHolder(ViewGroup parent, int viewType);

    /** 当viewHolder被重用时，调用该方法 */
    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        //由holder自己更新数据
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /** 往RecycleView中添加item */
    public void add(int pos, T item) {
        mData.add(pos, item);
        notifyItemInserted(pos);
    }

    /** 删除RecycleView中指定的item */
    public void delete(int pos) {
        mData.remove(pos);
        notifyItemRemoved(pos);
    }

    /** 删除RecycleView中指定的item */
    public void delete(T pos) {
        mData.remove(pos);
        notifyDataSetChanged();
    }

    /** 重新指定RecycleView的数据 */
    public void setItemDataList(List<T> data){
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    //设置item的点击侦听器
    private OnItemClickListener listener;

    /** 设置item的点击侦听器 */
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mClickListener = listener;
    }

    /** 设置item的长按侦听器 */
    public void setOnLongItemListener(OnLongItemListener listener){
        this.mLongListener = listener;
    }

    /** item的点击侦听器 */
    public interface OnItemClickListener{
        /**
         * item的点击回调
         * @param position
         */
        void onItemClick(View itemView, int position);

    }

    /** item的长按侦听器 */
    public interface OnLongItemListener{
        /**
         * item的长按回调
         * @param position
         */
        void onLongItemClick(View itemView, int position);

    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
